<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <!-- <el-input v-model="select_word" placeholder="输入类名" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button> -->
        </div>
        <div>
            <el-table :data="typeData" fit style="width: 100%">
                <el-table-column prop="name" label="分类名称">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="editFoo(scope.row)" type="text" size="small">编辑子分类</el-button>
                        <!-- <el-button type="text" size="small" @click="delFOO(scope.row)">删除</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <!-- <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page="page.number+1" layout="total,  prev, pager, next, jumper" :total="page.totalElements">
                </el-pagination>
            </div> -->
        </div>
        <el-dialog title="分类管理" :visible.sync="dialogTableVisible">
            <el-table :data="dataObj.gridData">
                <el-table-column property="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="editChild(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="delChild(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog title="新增" :visible.sync="innerVisible" append-to-body>
                <el-form :model="form">
                    <el-form-item label="文章分类">
                        <el-input v-model="form.name" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="innerVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addType">确 定</el-button>
                </div>
            </el-dialog>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogTableVisible = false">取 消</el-button>
                <el-button type="primary" @click="addnew(dataObj.id)">新 增</el-button>
            </div>
        </el-dialog>
    </div>
    </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import { firstType, addArticleType, delArticleType } from '../../server/getDate.js'
export default {
    data() {
        return {
            select_word: '',
            dialogTableVisible: false,
            innerVisible: false,
            dataObj: {
                id: 0,
                gridData: []
            },
            form: {

            }
        }
    },
    created() {
        this.getFirst()
    },
    computed: mapState({
        typeData: state => state.Article.firstType,
        user: state => state.Login.user
    }),
    methods: {
        ...mapActions(['save_first']),
        getFirst() {
            const self = this
            firstType()
                .then(res => {
                    console.log('res', res)
                    self.save_first(res)
                })
        },
        delFoo(row) {

        },
        editFoo(row) {
            this.dialogTableVisible = true
            let parentId = row.id
            console.log('parentId', parentId)
            let userId = this.user.id
            firstType({ parentId, userId })
                .then(res => {
                    console.log('res', res)
                    if (res.status === 0) {
                        this.dataObj.gridData = res.result
                        this.dataObj.id = parentId
                    } else {
                        this.$message.error('服务器异常')
                    }
                }, err => this.$message.error('服务器异常'))
        },
        editChild(row) {
            console.log('child', row)
            this.innerVisible = true
            this.form = row
        },
        delChild(row) {
            console.log('row', row)
            let id = row.id
            delArticleType({ id })
                .then(res => {
                    if (res.status === 0) {
                        this.dialogTableVisible = false
                        this.innerVisible = false
                        this.$message.info('删除成功')
                    } else {
                        this.$message.error('删除失败')
                    }
                }, err => this.$message.error('删除失败'))
        },
        handleCurrentChange(val) {

        },
        search() {

        },
        addType() {
            let articleSortEntity = this.form
            articleSortEntity.parentId = this.dataObj.id
            let userId = this.user.id
            console.log('art', articleSortEntity)
            addArticleType({ articleSortEntity, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.innerVisible = false
                        this.dialogTableVisible = false
                        this.$message.info('添加成功')
                    } else {
                        this.$message.error('添加失败')
                    }
                }, err => this.$message.error('添加失败'))
        },
        addnew(val) {
            this.innerVisible = true
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>